package com.movecar.ui;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.movecar.R;
import com.movecar.model.CloudBeansBean;
import com.movecar.model.Pictures;
import com.movecar.model.ThemeMatchDetailBean;
import com.movecar.utils.Alert;
import com.movecar.utils.AppConfig;
import com.movecar.utils.DataStore;
import com.movecar.utils.HttpPostRequestAsyncTask;
import com.movecar.utils.RequestResultCallback;
import com.movecar.utils.imageloader.Constants;
import com.movecar.widget.CustomProgressDialog;
import com.nostra13.universalimageloader.core.ImageLoader;

/**
 * 俱乐部详情
 * 
 * @author Zhao.JQ
 * 
 */
public class ThemeMatchDetailActivity extends Activity implements OnClickListener {

	private TextView description;
	private ArrayList<Pictures> picturesData = null;
	private ImageView match_img;
	private ScrollView mScrollView;
	private ImageLoader imageLoader = null;
	private CustomProgressDialog progressDialog = null;
	private CloudBeansBean mCloudBeansBean = null;
	private int position;
	private Button btn_right;
	private String ThemeId, isRelate;
	private ThemeMatchDetailBean mThemeMatchDetailBean;
	private LinearLayout main_linear;

	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.theme_match_detail);
		position = getIntent().getIntExtra("position", 0);
		ThemeId = getIntent().getStringExtra("ThemeId");
		isRelate = getIntent().getStringExtra("isRelate");
		initView();
	}

	private void initView() {
		imageLoader = ImageLoader.getInstance();
		// imageLoader.init(Constants.imageLoaderConfiguration(this));
		stopProgressDialog();
		mScrollView = (ScrollView) findViewById(R.id.mScrollView);
		main_linear = (LinearLayout) findViewById(R.id.main_linear);
		Button btn_left = (Button) findViewById(R.id.btn_left);
		btn_left.setVisibility(View.VISIBLE);
		btn_left.setOnClickListener(this);
		btn_right = (Button) findViewById(R.id.btn_right_long);
		btn_right.setBackgroundResource(R.drawable.btn_theme_joined);
		btn_right.setVisibility(View.GONE);
		match_img = (ImageView) findViewById(R.id.match_img);
		description = (TextView) findViewById(R.id.description);
		TextView title = (TextView) findViewById(R.id.title);
		title.setText("详情");
		startProgressDialog();
		getDetail();

	}

	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.btn_left:

			finish();

			break;

		default:
			break;
		}

	}

	@SuppressWarnings("unchecked")
	private void getDetail() {

		List<NameValuePair> params = new ArrayList<NameValuePair>(); //
		// 使用NameValuePair来保存要传递的Post参数
		params.add(new BasicNameValuePair("cmd", "queryOnePmNew1")); // 添加要传递的参数
		params.add(new BasicNameValuePair("userId", DataStore.getInstance().getUserId()));
		params.add(new BasicNameValuePair("pmId", ThemeId));
		new HttpPostRequestAsyncTask(new RequestResultCallback() {

			@Override
			public void onSuccess(Object object) {
				stopProgressDialog();
				String result = (String) object;

				Gson gson = new Gson();
				try {
					mThemeMatchDetailBean = gson.fromJson(result, ThemeMatchDetailBean.class);
				} catch (Exception e) {
					return;
				}
				if (null != mThemeMatchDetailBean) {
					stopProgressDialog();
					main_linear.setVisibility(View.VISIBLE);
					imageLoader.displayImage(AppConfig.HOST + mThemeMatchDetailBean.getLogo(), match_img,
							Constants.image_display_options, AppConfig.animateListener);

					description.setText(mThemeMatchDetailBean.getContent());
				} else {
					stopProgressDialog();
					Alert.error(ThemeMatchDetailActivity.this, "系统异常");
				}

			}

			@Override
			public void onFail(String exception) {

				stopProgressDialog();
				Alert.error(ThemeMatchDetailActivity.this, "与服务器连接失败，请检查网络");

			}
		}, ThemeMatchDetailBean.class, AppConfig.USER).execute(params);
	}

	private void startProgressDialog() {
		if (progressDialog == null) {
			progressDialog = CustomProgressDialog.createDialog(this);
			progressDialog.setMessage("正在加载...");
		}

		progressDialog.show();
	}

	private void stopProgressDialog() {
		if (progressDialog != null) {
			progressDialog.dismiss();
			progressDialog = null;
		}
	}

	public boolean dispatchKeyEvent(KeyEvent event) {

		if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN
				&& event.getRepeatCount() == 0) {

			finish();
			return false;
		} else {
			return super.dispatchKeyEvent(event);
		}
	}
}
